<template>
  <div class="warpper">
    <van-search
      v-model="value"
      placeholder="请输入影城名称"
      action-text="取消"
      :show-action="true"
      class="search"
      @cancel="onCancel"
      :change="onUpdate()"
    />
    <div class="none-data" v-if="newCinemasList.length <= 0">
      <p>离你最近</p>
      <ul>
        <li v-for="(data, index) in nearList" :key="index">{{ data.name }}</li>
      </ul>
    </div>
    <van-list class="vanlist" v-if="newCinemasList">
      <van-cell
        class="vancell"
        v-for="(data, index) in newCinemasList"
        :key="index"
      >
        <p class="name">
          {{ data.name }} <span>￥{{ data.lowPrice / 100 }}起</span>
        </p>
        <p class="address">{{ data.address }}</p>
      </van-cell>
    </van-list>
  </div>
</template>
<script>
import { Search, List, Cell } from "vant";
import Vue from "vue";
import { mapState } from "vuex";
Vue.use(Search).use(List).use(Cell);
export default {
  data() {
    return {
      value: "",
      newCinemasList: [],
      nearList: [],
    };
  },
  computed: {
    ...mapState("CinemaModule", ["cinemasList"]),
    newCinemasList() {
      if (this.value === "") {
    this.newCinemasList=[];
        return;
      }
      return this.cinemasList.filter((item) => {
        if (item.name.toUpperCase().includes(this.value.toUpperCase())||item.address.toUpperCase().includes(this.value.toUpperCase())) {
          return item;
        }
      });
    },
  },
  watch: {
    value(val) {
      if (val === "") {
        this.newCinemasList = [];
        return;
      }
      this.newCinemasList = this.cinemasList.filter((item) => {
        if (
          item.name.toUpperCase().includes(this.value.toUpperCase()) ||
          item.address.toUpperCase().includes(this.value.toUpperCase())
        ) {
          return item;
        }
      });
    },
  },
  methods: {
    //报错  You may have an infinite update loop in a component render function.
    onUpdate() {
      //   if (this.value === "") {
      //         return;
      //       }
      //       this.newCinemasList= this.cinemasList.filter((item) => {
      //         if (item.name.toUpperCase().includes(this.value.toUpperCase())||item.address.toUpperCase().includes(this.value.toUpperCase())) {
      //           return item;
      //         }
      //       });
    },
    onCancel() {
      this.$router.back();
    },
  },
  mounted() {
    this.$store.commit("hideTab");
    this.nearList = this.cinemasList.splice(0, 5);
  },
  destroyed() {
    this.$store.commit("showTab");
  },
};
</script>
<style lang="less" scoped>
.warpper {
  .search {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9999;
  }
  .none-data {
    margin-top: 80px;
    p {
      padding: 0 15px 8px 0;
      font-size: 13px;
      line-height: 13px;
      color: #bdc0c5;
      margin: 18px;
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      margin-left: 20px;
      li {
        height: 30px;
        background-color: hsla(0, 0%, 95.7%, 0.6);
        line-height: 14px;
        border-radius: 3px;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        font-size: 13px;
        margin: 4px 4px;
        padding: 4px 8px;
      }
    }
  }
  .vanlist {
    margin-top: 40px;
    padding: 10px;
    .vancell {
      .name {
        color: #191a1b;
        font-size: 15px;
        span {
          float: right;
          font-size: 15px;
          color: #ff5f16;
          margin-right: 10px;
        }
      }
      .address {
        max-width: 80%;
        overflow: hidden;
        -o-text-overflow: ellipsis;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #797d82;
        font-size: 12px;
        margin-top: 5px;
      }
    }
  }
}
</style>